/******************************************************************************
Paper: The Impact of Welfare on Intergroup Relations
Author: Akshay Dixit

Survey: This .do file produces the following main results using the survey data:
	- Table 4 (Average Effect of RBS on Borrowing from Caste Members)
	- Table 5 (The Effect of RBS on Borrowing from Caste Members and Festival Spending, by Inequality in Land Ownership)
	- Figure 6 (The Effect of Welfare on Intercaste Ties)
	- Figure 7 (Inequality and the Effect of Welfare on Intercaste Ties)
	- Figure 8 (The Effect of Welfare on Attitudes Towards Other Castes)

The .do file also produces more detailed versions of the above main results:
	- Table S51 (detailed version of Table 4)
	- Tables S10 and S15 (detailed version of Table 5)
	- Tables S3 and S4 (tabular versions of Figure 6)
	- Tables S23-S26 (tabular versions of Figure 7)
	- Tables S5 and S27 (tabular versions of Figure 8)
		
Finally, this .do file produces the following supplementary results:
	- Figure S5, Table S28 (Effect of welfare on preferred attributes of MLA candidates)
	- Table S13 (Effect on perceptions of inequality)
		
******************************************************************************/

clear all
gl data "$identity/data/survey"

* Remove existing .tex files, to be replaced by new ones ***
local datafiles: dir "$analysis/survey_main" files "*.tex"
foreach datafile of local datafiles {
        cap erase "$analysis/survey_main/`datafile'"
}

u "$data/Household Survey V2_clean.dta", clear

cd "$analysis/survey_main"

* Controls
gl controls1 = "hh_size village_population castes_in_village"
gl controls2 = "hh_size village_population castes_in_village other_welfare"

	/* 
	The addition or removal of "other_welfare" as a control doesn't change the results,
	but keep it in the analysis to get at the effect of RBS more precisely.
	*/

*******************************************************************************

*** Base specification: Graphs ****

* Inter-caste behavior

local intercaste_behavior share_meal share_meal_others friends_same_caste
foreach var of local intercaste_behavior {
	
	// Standardizing using the mean and s.d. of Andhra Pradesh
	qui sum `var' if (telangana == 0)
	g `var'_sd = (`var'-r(mean))/r(sd)
	
	// Running regression on the transformed outcome
	qui reg `var'_sd telangana_ownagriland telangana ownagriland $controls2, vce(cluster village)
	local b`var'_sd = _b[telangana_ownagriland]
	estimates store e_`var'
	
}

	// Repeating the above for donation outcome
qui sum donation if (telangana == 0 & scheduled_caste == 0)
g donation_sd = (donation-r(mean))/r(sd)
	
qui reg donation_sd telangana_ownagriland telangana ownagriland $controls2 if scheduled_caste == 0, vce(cluster village)
local bdonation_sd = _b[telangana_ownagriland]
estimates store e_donation

	// Coefficient plot 
#delimit ;
coefplot (e_donation, aseq("Donation (non-SCs)")
		\ e_share_meal, aseq("Meals with other castes")
		\ e_share_meal_others, aseq("Others have meals with other castes") 
		\ e_friends_same_caste, aseq("Most/all friends from own caste"))
	, keep(telangana_ownagriland) xline(0) swapnames title("", size(medium))
	  xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.6(0.2)0.6)
;	

#delimit cr

graph export "intercaste_behavior.pdf", as(pdf) replace
drop _est_e_*


* Inter-caste attitudes

local intercaste_attitudes caste_trust caste_neighbor caste_neighbor_others
foreach var of local intercaste_attitudes {
	
	// Standardizing using the mean and s.d. of Andhra Pradesh
	qui sum `var' if (telangana == 0)
	g `var'_sd = (`var'-r(mean))/r(sd)
	
	// Running regression on the transformed outcome
	qui reg `var'_sd telangana_ownagriland telangana ownagriland $controls2, vce(cluster village)
	estimates store e_`var'
}

	// Coefficient plot
#delimit ;
coefplot (e_caste_trust, aseq("Trust other castes")
		\ e_caste_neighbor, aseq("Willing to have other caste neighbor")
		\ e_caste_neighbor_others, aseq("Others willing to have other caste neighbor"))
	, keep(telangana_ownagriland) xline(0) swapnames title("", size(medium))
	  note("Note: Displaying 95% confidence intervals", size(vsmall)) xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.6(0.2)0.6)
;	

#delimit cr

graph export "intercaste_attitudes.pdf", as(pdf) replace
drop _est_e_*

******************************************************************************

*** Base specification: Tables ***

* Inter-caste behavior

qui summ donation if telangana == 0 & scheduled_caste == 0
local donation_mean = (r(mean))

qui reg donation telangana_ownagriland telangana ownagriland $controls2 if scheduled_caste == 0, vce(cluster village)
outreg2 using "donation.tex", append label nocons nor2 addstat(Andhra Pradesh mean, `donation_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")

local intercaste_behavior share_meal share_meal_others friends_same_caste
foreach var of local intercaste_behavior {
	
	qui summ `var' if telangana == 0
	local `var'_mean = (r(mean))
	
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2, vce(cluster village)
	outreg2 using "intercaste_behavior.tex", append label nocons nor2 addstat(Andhra Pradesh mean, ``var'_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")

}

* Inter-caste attitudes

local intercaste_attitudes caste_trust caste_neighbor caste_neighbor_others
foreach var of local intercaste_attitudes {
	
	qui summ `var' if telangana == 0
	local `var'_mean = (r(mean))
	
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2, vce(cluster village)
	outreg2 using "intercaste_attitudes.tex", append label nocons nor2 addstat(Andhra Pradesh mean, ``var'_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")
	
}

*******************************************************************************

*** Het. effects by inequality in land ownership (Land-to-population ratio of dominant caste): Graphs ***

* Inter-caste behavior

	// Donation: Regression
qui reg donation_sd telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1 & scheduled_caste == 0, vce(cluster village)
local bdonation_sd_l = _b[telangana_ownagriland]
estimates store e_donation_low

qui reg donation_sd telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0 & scheduled_caste == 0, vce(cluster village)
local bdonation_sd_h = _b[telangana_ownagriland]
estimates store e_donation_high

	// Donation: Effect size
qui sum donation if (telangana == 0 & scheduled_caste == 0)
local mean_donation = r(mean)
	
qui reg donation telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1 & scheduled_caste == 0, vce(cluster village)
	local bdonation_l = _b[telangana_ownagriland]
	local value_donation_l = (`bdonation_l'/`mean_donation')
	dis as error `value_donation_l'
	local es_donation_l : di %5.2f (`value_donation_l' * 100)
	local p_donation_l "`es_donation_l'%"

qui reg donation telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0 & scheduled_caste == 0, vce(cluster village)
	local bdonation_h = _b[telangana_ownagriland]
	local value_donation_h = (`bdonation_h'/`mean_donation')
	dis as error `value_donation_h'
	local es_donation_h : di %5.2f (`value_donation_h' * 100)
	local p_donation_h "`es_donation_h'%"

	// Other outcomes: Regression
local intercaste_behavior share_meal share_meal_others friends_same_caste
foreach var of local intercaste_behavior {
	
	qui reg `var'_sd telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
	local b`var'_sd_l = _b[telangana_ownagriland]
	estimates store e_`var'_low
	
	qui reg `var'_sd telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
	local b`var'_sd_h = _b[telangana_ownagriland]
	estimates store e_`var'_high

}

	// Other outcomes: Effect size
local intercaste_behavior share_meal share_meal_others friends_same_caste
foreach var of local intercaste_behavior {
	
	qui sum `var' if (telangana == 0)
	local mean_`var' = r(mean)
	
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
		local b`var'_l = _b[telangana_ownagriland]
		local value_`var'_l = (`b`var'_l'/`mean_`var'')
		dis as error `value_`var'_l'
		local es_`var'_l : di %5.2f (`value_`var'_l' * 100)
		local p_`var'_l "`es_`var'_l'%"
	
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
		local b`var'_h = _b[telangana_ownagriland]
		local value_`var'_h = (`b`var'_h'/`mean_`var'')
		dis as error `value_`var'_h'
		local es_`var'_h : di %5.2f (`value_`var'_h' * 100)
		local p_`var'_h "`es_`var'_h'%"
	
}

#delimit ;
coefplot (e_donation_low, aseq("Donation (non-SCs)")
		\ e_share_meal_low, aseq("Meals with other castes")
		\ e_share_meal_others_low, aseq("Others have meals with other castes") 
		\ e_friends_same_caste_low, aseq("Most/all friends from own caste"))
	, keep(telangana_ownagriland) xline(0) swapnames title("Lower land inequality", size(medium))
	  xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.8(0.2)0.8)
;

graph save intercaste_low, replace;

coefplot (e_donation_high, aseq("Donation (non-SCs)")
		\ e_share_meal_high, aseq("Meals with other castes")
		\ e_share_meal_others_high, aseq("Others have meals with other castes") 
		\ e_friends_same_caste_high, aseq("Most/all friends from own caste"))
	, keep(telangana_ownagriland) xline(0) swapnames title("Higher land inequality", size(medium))
	  xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.8(0.2)0.8) yscale(off) fxsize(47.5) mcolor(gs10) ciopts(lcolor(gs10))
;

graph save intercaste_high, replace;

#delimit cr

graph combine "intercaste_low" "intercaste_high", cols(2)

graph export "het_inequality_intercaste_behavior.pdf", as(pdf) replace
erase intercaste_low.gph
erase intercaste_high.gph
drop _est_e_*


* Inter-caste attitudes

local intercaste_attitudes caste_neighbor caste_neighbor_others caste_trust
foreach var of local intercaste_attitudes {
	
	qui reg `var'_sd telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
	estimates store e_`var'_l
	
	qui reg `var'_sd telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
	estimates store e_`var'_h

}

#delimit ;
coefplot (e_caste_trust_l, aseq("Trust other castes")
		\ e_caste_neighbor_l, aseq("Willing to have other caste neighbor")
		\ e_caste_neighbor_others_l, aseq("Others willing to have other caste neighbor"))
	, keep(telangana_ownagriland) xline(0) swapnames title("Lower land inequality", size(medium))
	  xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.8(0.2)0.8)
;

graph save intercaste_low, replace;

coefplot (e_caste_trust_h, aseq("Trust other castes")
		\ e_caste_neighbor_h, aseq("Willing to have other caste neighbor")
		\ e_caste_neighbor_others_h, aseq("Others willing to have other caste neighbor"))
	, keep(telangana_ownagriland) xline(0) swapnames title("Higher land inequality", size(medium))
	  xtitle("Effect Size (Standard Deviations)", size(small))
	  xlabel(-0.8(0.2)0.8) yscale(off) fxsize(45) mcolor(gs10) ciopts(lcolor(gs10))
;

graph save intercaste_high, replace;

#delimit cr

graph combine "intercaste_low" "intercaste_high", cols(2)

graph export "het_inequality_intercaste_attitudes.pdf", as(pdf) replace
erase intercaste_low.gph
erase intercaste_high.gph
drop _est_e_*

*******************************************************************************

*** Het. effects by inequality in land ownership (Land-to-population ratio of dominant caste): Tables ***

* Inter-caste behavior

qui summ donation if telangana == 0 & scheduled_caste == 0
local donation_mean = (r(mean))

qui reg donation telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1 & scheduled_caste == 0, vce(cluster village)
outreg2 using "inequality_no_donation.tex", append label nocons nor2 addstat(Andhra Pradesh mean, `donation_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")

qui reg donation telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0 & scheduled_caste == 0, vce(cluster village)
outreg2 using "inequality_yes_donation.tex", append label nocons nor2 addstat(Andhra Pradesh mean, `donation_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")

local intercaste_behavior share_meal share_meal_others friends_same_caste
foreach var of local intercaste_behavior {
	
	qui summ `var' if telangana == 0
	local `var'_mean = (r(mean))
	
	dis as error "Low inequality"
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
	outreg2 using "inequality_no_intercaste_behavior.tex", append label nocons nor2 addstat(Andhra Pradesh mean, ``var'_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")
	
	dis as error "High inequality"
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
	outreg2 using "inequality_yes_intercaste_behavior.tex", append label nocons nor2 addstat(Andhra Pradesh mean, ``var'_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")

}

* Inter-caste attitudes

local intercaste_attitudes caste_trust caste_neighbor caste_neighbor_others
foreach var of local intercaste_attitudes {
	
	qui summ `var' if telangana == 0
	local `var'_mean = (r(mean))
	
	dis as error "Low inequality"
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
	outreg2 using "inequality_no_intercaste_attitudes.tex", append label nocons nor2 addstat(Andhra Pradesh mean, ``var'_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")
	
	dis as error "High inequality"
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
	outreg2 using "inequality_yes_intercaste_attitudes.tex", append label nocons nor2 addstat(Andhra Pradesh mean, ``var'_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")
	
}

*******************************************************************************

*** Impact on borrowing from caste members ***

qui summ borrowed_rel_caste if telangana == 0
local borrowed_rel_caste_mean = (r(mean))

qui reg adj_borrowed_rel_caste telangana_ownagriland telangana ownagriland $controls2, vce(cluster village)
outreg2 using "borrowed_caste.tex", append label nocons nor2 ctitle("Full sample") addstat(Andhra Pradesh mean, `borrowed_rel_caste_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS") 

qui reg adj_borrowed_rel_caste telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
outreg2 using "borrowed_caste.tex", append label nocons nor2 ctitle("Low inequality") addstat(Andhra Pradesh mean, `borrowed_rel_caste_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS") 

qui reg adj_borrowed_rel_caste telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
outreg2 using "borrowed_caste.tex", append label nocons nor2 ctitle("High inequality") addstat(Andhra Pradesh mean, `borrowed_rel_caste_mean') addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS") 

******************************************************************************

*** Impact on festival spending ***

qui summ festival_spending if telangana == 0
local festival_spending_mean = (r(mean))

qui reg adj_festival_spending telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
outreg2 using "festival_spending.tex", append label nocons nor2 addstat(Andhra Pradesh mean, `festival_spending_mean') ctitle("Low inequality") addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS") 

qui reg adj_festival_spending telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
outreg2 using "festival_spending.tex", append label nocons nor2 addstat(Andhra Pradesh mean, `festival_spending_mean') ctitle("High inequality") addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS") 

******************************************************************************

*** MLA candidate preference ***

	/*
		Omit "Honesty/No corruption" from this list because almost everyone
		(93%) said that was important to them.
		
		Omit "candidate's party" and "welfare schemes supported by the candidate/candidate's party"
		as these are party- rather than candidate-specific variables.
	*/

lab var q8p19_5 "Education"
lab var q8p19_7 "Benefits to family"
lab var q8p19_8 "Benefits to caste"
lab var q8p19_2 "Candidate's caste"
lab var q8p19_3 "Experience/seniority"
	
local candidate q8p19_5 q8p19_7 q8p19_8 q8p19_2 q8p19_3
foreach var of local candidate {
	qui reg `var' telangana_ownagriland telangana ownagriland $controls2, vce(cluster village)
	estimates store e_`var'
	
	outreg2 using "candidate_preference.tex", append label nocons nor2 
}

#delimit ;

coefplot (e_q8p19_5, aseq("Education")
		\ e_q8p19_7, aseq("Benefits to family")
		\ e_q8p19_8, aseq("Benefits to caste")
		\ e_q8p19_2, aseq("Candidate's caste")
		\ e_q8p19_3, aseq("Experience/seniority"))
	, keep(telangana_ownagriland) xline(0) swapnames title("", size(medium))
	  note("Note: Displaying 95% confidence intervals", size(vsmall)) xtitle("Effect size (percentage points)", size(small))
	  xlabel(-0.3(0.1)0.3)
;	

#delimit cr

graph export "candidate_preference.png", as(png) replace
drop _est_e_*

******************************************************************************

*** Perceptions of inequality ***

qui reg inequality_increased telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 1, vce(cluster village)
outreg2 using "inequality_increased.tex", append label nocons nor2 ctitle("Low inequality") addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS") 

qui reg inequality_increased telangana_ownagriland telangana ownagriland $controls2 if low_land_inequality == 0, vce(cluster village)
outreg2 using "inequality_increased.tex", append label nocons nor2 ctitle("High inequality") addnote("Controls: Number of HH members, village population, number of castes in village, benefits from welfare programs other than RBS")

******************************************************************************

local datafiles: dir "$analysis/survey_main" files "*.txt"
foreach datafile of local datafiles {
        cap erase "$analysis/survey_main/`datafile'"
}

clear	

	
	